package com.bdqn.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdqn.model.Users;
import com.bdqn.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@RequestMapping("/login")
	public String login(Users user,HttpServletRequest request) {
		
		HttpSession session = request.getSession();
		
		Users u = userService.login(user);
		
	
		session.setAttribute("user",u);
		
		if(u!=null) {
			return "index";
		}
	
	return "redirect:/";
	}
	@RequestMapping("/changePassword")
	@ResponseBody
	public int changePassword(Users user,Model model) {
		
		int num= userService.update(user);

	return num;
	}
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request) {
		
		request.removeAttribute("users");	
	
	return "redirect:/";
	}
}

